const mapRoutes = [{
  path: 'add_routes',
  title: '动态路由',
  icon: 'nested',
}, {
  path: 'theater',
  title: '剧目管理',
  icon: 'nested',
}, {
  path: 'hahaha',
  title: '我是哈哈组件',
  icon: 'nested',
}, {
  path: 'user',
  title: '用户管理',
  icon: 'nested',
  children: [{
    path: 'pu',
    title: '普通会员'
  }, {
    path: 'wen',
    title: '文慧卡会员'
  }, ]
}]


function createAsyncRoutes(routesSource) {
  const asyncRoutes = []
  if (!routesSource || !Array.isArray(routesSource)) {
    return []
  }
  routesSource.forEach(item => {
    if (!item.children || item.children == []) {
      const child = creatChild1(item)
      asyncRoutes.push({
        path: '/' + item.path,
        component: 'Layout',
        children: child,
        name: 'Layout',
        hidden: !item.hidden || item.hidden == '0' ? false : true
      })
    } else if (item.children && item.children.length > 0) {
      let url = item.path
      asyncRoutes.push({
        path: '/' + item.path,
        component: 'Layout',
        name: 'Layout',
        meta: {
          title: item.title,
          icon: item.icon
        },
        children: createChildren(item.children, url), //第二层
        hidden: !item.hidden || item.hidden == '0' ? false : true
      })
    }
  })

  // asyncRoutes.push({
  //   path: '*',
  //   redirect: '/404',
  //   hidden: true
  // })

  return asyncRoutes
}

function creatChild1(item) {
  const arr = []
  arr.push({
    path: 'index',
    name: item.path.charAt(0).toUpperCase() + item.path.slice(1),
    component: `@/views/wenhuahui/${item.path}`,
    // component: resolve => require([`@/views/wenhuahui/${item.path}`], resolve),
    meta: {
      title: item.title,
      icon: item.icon,
      btn: item.btn ? Object.assign({}, item.btn) : ''
    },
    hidden: !item.hidden || item.hidden == '0' ? false : true
  })
  return arr
}

function createChildren(grandChildren, path) {
  const newArr = []
  grandChildren.forEach(child => {
    if (!child.children || child.children == []) {
      let lujin = path + '/' + child.path
      newArr.push({
        path: child.path,
        name: child.path.charAt(0).toUpperCase() + child.path.slice(1),
        component: `@/views/wenhuahui/${lujin}`,
        // component: resolve => require([`@/views/wenhuahui/${lujin}`], resolve),
        meta: {
          title: child.title,
          btn: child.btn ? Object.assign({}, child.btn) : ''
        },
        hidden: !child.hidden || child.hidden == '0' ? false : true
      })
    } else if (child.children && child.children.length > 0) {
      let lujin = path + '/' + child.path
      const grandSon = createGrandson(child.children, lujin)
      newArr.push({
        path: child.path,
        name: child.path.charAt(0).toUpperCase() + child.path.slice(1),
        component: `@/views/wenhuahui/${lujin}`,
        // component: resolve => require([`@/views/wenhuahui/${lujin}`], resolve),
        meta: {
          title: child.title,
          btn: child.btn ? Object.assign({}, child.btn) : ''
        },
        children: grandSon,
        hidden: !child.hidden || child.hidden == '0' ? false : true
      })
    }
  })
  return newArr
}

function createGrandson(prad, path) {
  let grandSonList = []
  prad.forEach(temp => {
    let lujin = path + '/' + temp.path
    grandSonList.push({
      path: temp.path,
      name: temp.path.charAt(0).toUpperCase() + temp.path.slice(1),
      component: `@/views/wenhuahui/${lujin}`,
      // component: resolve => require([`@/views/wenhuahui/${lujin}`], resolve),
      meta: {
        title: temp.title,
        btn: temp.btn ? Object.assign({}, temp.btn) : ''
      },
      hidden: !temp.hidden || temp.hidden == '0' ? false : true
    })
  })
  return grandSonList
}


const rout = createAsyncRoutes(mapRoutes)
// console.log(rout[0].children[0].component, 'wanshang')

export default rout
